package com.zat.mes.andrmes.dofun.system.fragment

import android.view.View
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.base.BaseFragment
import com.zat.mes.andrmes.data.ClockBean
import com.zat.mes.andrmes.dofun.system.ClockInReadActivity
import com.zat.mes.andrmes.dofun.system.adapter.ClockReadAdapter
import com.zat.mes.andrmes.dofun.system.vm.ClockReadViewModel
import com.zat.mes.andrmes.event.CheckInUnEventMessage
import kotlinx.android.synthetic.main.fragment_clockin_read.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * @author zy
 * @date :2020-02-16 18:36
 * @description: 取消审核
 */
class ClockInUnReadFragment : BaseFragment(), View.OnClickListener {

    private lateinit var clockModel: ClockReadViewModel
    lateinit var clockReadAdapter: ClockReadAdapter
    lateinit var clockReadActivity: ClockInReadActivity
    override fun getViewId(): Int {
        return R.layout.fragment_clockin_read
    }

    override fun isOpenTitleBar(): Boolean {
        return false
    }

    /**
     * 初始化view
     */
    override fun initView() {
        recylerClockReadFragment.layoutManager = LinearLayoutManager(context)
        btnClockReadFragment.text = getString(R.string.text_cancel_confirm)
        llSelectClockReadFragment.setOnClickListener(this)
        btnClockReadFragment.setOnClickListener(this)
    }

    /**
     * 初始化data
     */
    override fun initData() {
        EventBus.getDefault().register(this)
        clockReadActivity = activity as ClockInReadActivity
        val clockBeanList: MutableList<ClockBean> = ArrayList()
        clockReadAdapter = ClockReadAdapter(R.layout.item_clockin_read_fragment,
            object : ClockReadAdapter.SelectFullCallBack {
                override fun selectFull(full: Boolean) {
                    if (full) {
                        setSelectBtn(true)
                    } else {
                        setSelectBtn(false)
                    }
                }

            })
        recylerClockReadFragment.adapter = clockReadAdapter
        clockReadAdapter.addData(clockBeanList)
        loadModel()
    }

    /**
     * 加载viewmodel
     */
    private fun loadModel() {
        clockModel = ViewModelProvider(this).get(ClockReadViewModel::class.java)
        clockModel.getClockReadList().observe(this, Observer {
            closeLoading()
            if (it != null && it.size > 0) {
                setEmptyGone()
            } else {
                setEmptyVisible()
            }
            clockReadAdapter.setNewData(it as MutableList<ClockBean>?)
        })
        clockModel.getClockReadData().observe(this, Observer {
            closeLoading()
            Toast.makeText(context, it, Toast.LENGTH_SHORT).show()
            setSelectBtn(false)
            clockReadAdapter.setSelectList(false)
            EventBus.getDefault().post(CheckInUnEventMessage())
        })
        request()

    }

    /**
     * 请求数据
     */
    fun request() {
        clockReadActivity.empId?.let {
            clockModel.loadClockReadList(clockReadActivity.selectedDate, it, "1")
        }
        showLoading()
    }

    /**
     * 点击事件
     */
    override fun onClick(v: View?) {
        when (v) {
            llSelectClockReadFragment -> {
                if (clockReadAdapter.data.size == 0) {
                    return
                }
                if (clockReadAdapter.selectList.size == clockReadAdapter.data.size) {
                    clockReadAdapter.setSelectList(false)
                    setSelectBtn(false)
                } else {
                    clockReadAdapter.setSelectList(true)
                    setSelectBtn(true)

                }
            }
            //提交
            btnClockReadFragment -> {
                if (clockReadAdapter.selectList.size <= 0) {
                    Toast.makeText(
                        context,
                        getString(R.string.text_select_please),
                        Toast.LENGTH_SHORT
                    ).show()
                } else {
                    showLoading()
                    clockModel.loadClockRead(clockReadAdapter.selectList, false)
                }
            }
        }


    }

    /**
     * 全选按钮
     */
    fun setSelectBtn(full: Boolean) {
        if (full) {
            ivSelectClockReadFragment.background =
                context?.let { it1 ->
                    ContextCompat.getDrawable(
                        it1,
                        R.mipmap.icon_kaoqin_xuanzhong
                    )
                }
        } else {
            ivSelectClockReadFragment.background =
                context?.let { it1 ->
                    ContextCompat.getDrawable(
                        it1,
                        R.mipmap.icon_kaoqin_weixuanzhong
                    )
                }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onCheckInEvent(event: CheckInUnEventMessage) {
        request()
    }

    override fun onDestroyView() {
        EventBus.getDefault().unregister(this)
        super.onDestroyView()
    }
}